package slidepuzzle.p.com.google.code;

import android.util.AttributeSet;
import android.view.View;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import java.lang.String;
import android.graphics.Rect;
import android.graphics.RectF;
// import android.view.View.MeasureSpec;

public class PuzzleView extends View {
	
	public PuzzleView (Context context){
		super(context);
		this.strPaint = new Paint();
	}
	
	public PuzzleView(Context context, AttributeSet attrs){
		super(context, attrs);
		this.strPaint = new Paint();
	}
	
	public PuzzleView(Context context, AttributeSet attrs, int defStyle){
		super(context, attrs, defStyle);
		this.strPaint = new Paint();
	}
	
	public PuzzleView(Context context, String str){
		super(context);
		this.setStr(str);
		this.strPaint = new Paint();
		this.rectPaint = new Paint();
		}
	
	@Override
	public void onDraw(Canvas canvas){
		canvas.drawColor(Color.YELLOW);
		this.setSorroundRectPaint();
		canvas.drawRoundRect (this.initSorroundRect(canvas), 7f, 7f, this.rectPaint);
		this.setStrPaint();
 		canvas.drawText( this.str, this.calcTextX(canvas), this.calcTextY(canvas), this.strPaint);	
	}	
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
		int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
		this.setMeasuredDimension( parentWidth/4, parentWidth/4);
	}
	
	private void setStr(String str){
		this.str = new String(str);
	}

	private int calcTextX(Canvas canvas){
		return canvas.getWidth()/2;
	}
	
	private int calcTextY(Canvas canvas){
		Rect r = new Rect();
		this.strPaint.getTextBounds(this.str, 0, this.str.length(), r);
		int i =  canvas.getHeight() - ((canvas.getHeight() - r.height())/2);
		return i;
	}
	
	private void setStrPaint(){
		this.strPaint.setColor(Color.RED);
		this.strPaint.setTextSize(this.getMeasuredHeight()*0.9f);
		this.strPaint.setTextAlign(Paint.Align.CENTER);
	}
	
	private void setSorroundRectPaint(){
		this.rectPaint.setColor(Color.BLACK);
		this.rectPaint.setStrokeWidth(5f);
		this.rectPaint.setStyle(Paint.Style.STROKE);
	}
	
	private RectF initSorroundRect(Canvas canvas){
		int x = 3;
		RectF sorroundRect = new RectF(x, x, canvas.getWidth() - x, canvas.getHeight() - x);
		return sorroundRect;
	}
	
	private Paint strPaint;
	private Paint rectPaint;
	private String str;
	// private Rect sorroundRect;
}